from flask import session
from flask_restful import Resource

from database import Database


# Mock user database
class GetCart(Resource):

    def get(self):
        db = Database()
        username = session.get('username')
        cart_all = db.execute_query("SELECT * FROM cart WHERE userId=%s", (username,))

        cart_json = []
        if cart_all:
            for item in cart_all:
                book = db.execute_query("SELECT * FROM book WHERE bookId=%s", (item[0],))
                book = book[0]
                stack = db.execute_query("SELECT * FROM stack WHERE stackId=%s", (book[3],))
                stack = stack[0]
                book_json = {
                    "bookId": book[0],
                    "categoryId": book[1],
                    "imageUrl": stack[2],
                    "title": stack[3],
                    "description": book[4],
                    "stackId": book[3],
                    "ownerId": book[4],
                    "condition": book[5],
                    "fee": str(book[6]),
                    "price": str(book[7]),
                    "author": stack[5],
                    "status": book[8]
                }
                cart_json.append(book_json)
            return {"cart": cart_json}, 200
        else:
            return {"message": "cart is null"}, 200
